package cn.kattomy.shop.categorysecond.adminaction;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.kattomy.shop.category.service.CategoryService;
import cn.kattomy.shop.category.vo.Category;
import cn.kattomy.shop.categorysecond.service.CategorySecondService;
import cn.kattomy.shop.categorysecond.vo.CategorySecond;
import cn.kattomy.shop.utils.PageBean;

public class AdminCategorySecondAction extends ActionSupport implements ModelDriven<CategorySecond>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	//模型驱动使用的对象
	private CategorySecond categorySecond = new CategorySecond();
	@Override
	public CategorySecond getModel() {
		return categorySecond;
	}
	
	//注入二级分类 Service
	private CategorySecondService categorySecondService;
	public void setCategorySecondService(CategorySecondService categorySecondService) {
		this.categorySecondService = categorySecondService;
	}
	
	//接收 PageNo
	private Integer pageNo;
	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	//注入一级分类的 Service
	private CategoryService categoryService;
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}


	//查询二级分类的方法
	public String findAll(){
		PageBean<CategorySecond> pageBean = categorySecondService.findByPage(pageNo);
		//将 pageBean 中的数据保存到值栈中
		ActionContext.getContext().getValueStack().set("pageBean", pageBean);
		return "findAll";
	}
	
	//跳转到添加页面
	public String addPage(){
		//查询所有的一级分类
		List<Category> cList = categoryService.getAll();
		//把数据显示到页面的下拉列表中
		ActionContext.getContext().getValueStack().set("cList", cList);
		//页面跳转
		return "addPageSuccess";
	}
	
	//保存二级分类的方法
	public String save(){
		categorySecondService.save(categorySecond);
		return "saveSuccess";
	}
	
	//删除二级分类的方法
	public String delete(){
		//如果级联删除，先查询再删除。配置 cascade
		categorySecond = categorySecondService.findByCsid(categorySecond.getCsid());
		categorySecondService.delete(categorySecond);
		ActionContext.getContext().getValueStack().set("pageNo", pageNo);
		return "deleteSuccess";
	}
	
	//编辑二级分类的方法
	public String edit(){
		//根据二级分类 csid 查询二级分类对象
		categorySecond = categorySecondService.findByCsid(categorySecond.getCsid());
		//查询所有的一级分类
		List<Category> cList = categoryService.getAll();
		ActionContext.getContext().getValueStack().set("cList", cList);
		return "editPageSuccess";
	}
	
	//修改二级分类的方法
	public String update(){
		categorySecondService.update(categorySecond);
		return "updateSuccess";
	}
	
}